 <?php

class IndexController extends Zend_Controller_Action
{

    public function init()
    {
        /* Initialize action controller here */
    }

    public function indexAction()
    {
        $posts=new Model_Posts();
                        $result = $posts->getPostswithCommnets();
                
                        $pagina=Zend_Paginator::factory($result);
                        $pagina->setCurrentPageNumber($this->_getparam('page',1));
                        $pagina->setItemCountPerPage(2);
                        $this->view->paginator = $pagina;
    }

    public function logoutAction()
    {
        Zend_Auth::getInstance()->clearIdentity();
                       //$this->_forward("logout");
                       //$this->_redirect("index/logout");
    }

    public function viewAction()
    {

            $posts= new Model_Posts();
            $id=(int)$this->_getParam('id');
            $result=$posts->getPost($id);

            if (Zend_Auth::getInstance()->hasIdentity()) {
                
            $comment_form= new Form_Comment();
            
            $userRole=Zend_Auth::getInstance()->getIdentity();
            $acCntrl=new Model_Acl();
            
            if ($acCntrl->isAllowed($userRole->Role, 'comments', 'add')) {

                if ($this->getRequest()->isPost()) {
                    if($comment_form->isValid($this->getRequest()->getPost())) {

                        $toId=$this->getRequest()->getPost('post_id');
                        $addCom= new Model_Comments();
                        $form_val=$comment_form->getValues();
                        $form_val['post_id']=$id;
                        $addCom->newComment($form_val);
                        $this->_redirect('admin/index');

                    }
                }
            
            } else {
                $comment_form="You are not Authorized to Post Comments";
            }

        } else {
            $comment_form="Please login to post Comments.";
        }

        $show_comm= new Model_Comments();
        $comments = $show_comm->getComment($id);
        $this->view->post = $result;
        $this->view->comment = $comment_form;
        $this->view->mess = $show_comm;
        $this->view->show_comment = $comments;
    }
}



